home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13205 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: newshost.cyberramp.net!news
  2. From: sinan@cyberramp.net (John L. Noland)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Recursion
  5. Date: 5 Apr 1996 02:08:48 GMT
  6. Organization: CyberRamp
  7. Message-ID: <4k1vbg$6gu@newshost.cyberramp.net>
  8. References: <31624BC2.70D2@sooner.net>
  9. NNTP-Posting-Host: ramp1-7.cyberramp.net
  10. X-Newsreader: WinVN 0.99.5
  11.  
  12. n article <31624BC2.70D2@sooner.net>, edwbush@sooner.net says...
  13. >
  14. >I am trying to construct a C function that will recursively convert
  15. >a string such as "1234" into it's integer equivelant (1234).
  16. >
  17. >Here is what I know:
  18. >1)if you subtract the character "0" from any of the other digits "1".."9"
  19. >  you will get the integer value of that characer.
  20. >        Example:  "1" - "0" is equal to 1
  21. >                  "2" - "0" is equal to 2
  22. >                  .
  23. >                  .
  24. >                  .
  25. >                  "9" - "0" is equal to 9
  26. >2)the function should be called with a character pointer:
  27. >        Such as:   convert("1234");
  28. >  making the prototype look something like:
  29. >        int convert(char *p);
  30. >
  31. >Does anyone have an idea?  This is sorta stumping me.  I am aware of 
  32. >atoi, but I am wanting to write a recursive function that does that -- 
  33. >for the fun of it.  It's sort of a little puzzle to help me learn 
  34. >recursion.  Any ideas?
  35. >
  36. >Thanx in Advance!
  37. >
  38.  
  39. void convert (char *buf, int result);
  40.  
  41. int main (void) {
  42.  
  43. char str[] = "1234";
  44. int  result = 0;
  45.  
  46.     convert(str, result);
  47.     return 0;
  48.  
  49. }
  50.  
  51. void convert (char *buf, int result) {
  52.  
  53. int check_array[100]; /* Good for strings of numbers up to 100 digits long! */  
  54. int i = 0, j = 0;
  55.  
  56.     if (*buf = '\0') return;
  57.     j = i;                              /*Save i */
  58.     for (i = 0; i < 100; i++) 
  59.         check_array[i] = 0;             /*Initialize our array */
  60.     i = j;                              /*Restore i */   
  61.     check_array[i] = (int)(*buf - '0'); /*Convert ASCII to integer and save */
  62.     i += 1;                             /*increment i  */
  63.     j = i;                              /*Save i for later*/
  64.     for (i = 0; i < 100; i++) 
  65.         result += check_array[i];       /*tally up the numbers */               
  66.     i = j;                              /*restore i to previous */
  67.     buf += i;                           /*advance our pointer */
  68.     convert(buf, result); 
  69.     if (*buf = '\0') return;
  70. }
  71.  
  72. This could probably be made a little more efficient, but I think this'll work 
  73. for you. Didn't test it though!
  74.  
  75. Helping with homework is good for the soul!
  76.  
  77. -John
  78.  
  79.  
  80.